#include "Limiter.h"

/**********************************************************************/
/* Constructor - creates limiter in specific position, computed       */
/*  from the parameters: width of the level and position in z-axis.   */
/**********************************************************************/
CLimiter::CLimiter(CBat* pBat, float fWidth, float fZpos)
	: m_pBatAttached(pBat)
{
	//non-ODE init routines:
	m_bMarked = false;	//not marked to delete
	m_jointTrack = 0;

	//ODE init routines:
	m_body = dBodyCreate( m_pPhysics->GetWorld() );
		
	m_geom = dCreateBox(m_pPhysics->GetSpace(), fWidth - 1.0, 2.0, 2.0);
		
	dGeomSetBody(m_geom, m_body);

	dMassSetBox(&m_mass, 1, fWidth - 1.0, 2.0f, 2.0f);
	dBodySetMass(m_body, &m_mass);

	dBodySetPosition(m_body, 0.0, 1.0, fZpos);

	//create track joint
	m_jointTrack = dJointCreateSlider(m_pPhysics->GetWorld(), 0);
	dJointAttach(m_jointTrack, m_body, 0);
	dJointSetSliderAxis(m_jointTrack, 1.0f, 0.0f, 0.0f);


	dBodySetData(m_body, this);
}

CLimiter::~CLimiter(void)
{
	dBodyDestroy(m_body);
	dGeomDestroy(m_geom);
}


/**********************************************************************/
/* Draw function is empty - limiter is invisible.                     */
/**********************************************************************/
int CLimiter::Draw()
{
	return 0;
}

/**********************************************************************/
/* Hit - called when limiter is hitted by a ball                      */
/**********************************************************************/
int CLimiter::Hit()
{
	m_pBatAttached->Fail();
	return 0;
}